package logic

import (
	"gameserver/gcommon"
	"gameserver/tools/TestRobot/cmd"
	"gameserver/tools/TestRobot/internal"
	"gameserver/tools/TestRobot/models"
	"time"

	"gitee.com/night-tc/gobige/logger"
	"gitee.com/night-tc/gobige/msgdef/protomsg"
	"gitee.com/night-tc/gobige/msghandler"
	"gitee.com/night-tc/gobige/sess"
)

func init() {
	cmd.GetCmdMgr().RegisterLogic(cmd.Logic_Gateway, "链接Gateway", (*LogicGateWay)(nil))
}

const (
	GwStep_Dial StepTypeEnum = iota
	GwStep_verifying
	GwStep_ClientVerify
	GwStep_EnterSpace
	GwStep_end
)

// 连接GateWay
type LogicGateWay struct {
	LogicBase
}

// 生成命令参数
func (this *LogicGateWay) GenCmdParam(cmd *cmd.CmdInfo) {
	//没有参数
}

// 解析命令 成功继续，失败显示命令参数 这里不能设置给this
func (this *LogicGateWay) ParseCmd(cmd *cmd.InputCmd) bool {
	//没有参数
	return true
}

// 销毁逻辑
func (this *LogicGateWay) Destroy() {
	if this.user.Gateway != nil {
		this.user.Gateway.MsgHander.ReKeyMsgProc(cmd.Logic_Gateway.String())
	}
}

// 类型 名字
func (this *LogicGateWay) GetName() string {
	return "LogicGateWay"
}

func (lg *LogicGateWay) Init(user *models.RobotUser) {
	lg.LogicBase.Init(user)
	lg.LogicBase.realPtr = lg
	lg.maxStep = GwStep_end

	lg.stepLi[GwStep_Dial] = lg.dialGateWay
	lg.stepLi[GwStep_verifying] = lg.verifying
}

/*
连接Gateway
*/
func (this *LogicGateWay) dialGateWay() {
	var err error

	gateway, err := sess.Dial("tcp", nil, this.user.GatewayAddr, internal.Thgo)
	if err != nil {
		logger.Error("dialGateWay error ", this.user.GatewayAddr, err)
		this.SetTimer(time.Second * 10)
		return
	}
	handler := msghandler.NewKeyHandler()
	gateway.SetMsgHandler(handler)
	this.user.Gateway = &models.RobotSession{
		INetSess:  gateway,
		MsgHander: handler,
	}
	// gateway.SetInputMsg(true)
	// gateway.SetRecvMsg(true)
	// logger.Info("Connected to", lg.user.GatewayAddr)
	this.SetNext(GwStep_Dial+1, 0)
	//这个是基础消息与Logic没有关系，用于填充数据
	this.user.Gateway.RegMsgProc("", &models.BaseMsgProc{User: this.user})

	this.user.Gateway.RegMsgProc(cmd.Logic_Gateway, &GatewayMsg{
		lg: this,
	})
	this.user.Gateway.Start()
}

// 验证用户token
func (lg *LogicGateWay) verifying() {
	lg.user.Verify.SessUID = lg.user.RolePID
	lg.user.Verify.SessType = 0
	lg.user.Verify.SessState = 1
	logger.Debugf(gcommon.LogKey_Robot, "pid:%d verifying %v ", lg.user.RolePID, lg.user.Verify)
	lg.user.ReqStartTime = time.Now().UnixNano()
	lg.user.Gateway.Send(lg.user.Verify)
	lg.SetNext(GwStep_verifying+1, 0)
}

type GatewayMsg struct {
	lg *LogicGateWay
}

// func (this *GatewayMsg) MsgProc_RPCMsg(content msgdef.IMsg) {
// 	msg := content.(*msgdef.RPCMsg)
// 	// 从客户端收到的消息要判断要调用的服务器类型
// 	logger.Debug(logger.LogKey_Robot, "GatewayMsg.MsgProc_RPCMsg: ", msg.MethodName)
// 	this.lg.user.Gateway.FireRPC(msg.MethodName, msg.Data)
// }

// func (this *GatewayMsg) MsgProc_LobbyHeroInfo(content msgdef.IMsg) {
// 	logger.Debug(internal.LogKey_Robot, "[GatewayMsg] MsgProc_LobbyHeroInfo: ", content)
// }

func (this *GatewayMsg) MsgProc_ClientVerifySucceedRet(ret *protomsg.ClientVerifySucceedRet) {
	this.lg.SetNext(GwStep_ClientVerify+1, 0)
	logger.Debug(gcommon.LogKey_Robot, "GatewayMsg.MsgProc_ClientVerifySucceedRet: ", ret)
}

// func (this *GatewayMsg) MsgProc_UserMainDataNotify(content msgdef.IMsg) {
// 	msg := content.(*protoMsg.UserMainDataNotify)
// 	if msg != nil {
// 		this.lg.user.EntityID = msg.EntityID
// 		// this.lg.SetStep(this.lg.Step + 1)
// 	}
// 	logger.Debug(internal.LogKey_Robot, "MsgProc_UserMainDataNotify:", content)
// }

// func (lg *GatewayMsg) MsgProc_MapPreloadNotice(content msgdef.IMsg) {
// 	// lg.lg.user.Send_GM("srvinput")
// 	// lg.lg.user.Send_GM("srvrecv")

// 	//	重新设置一个等待时间-从这个时候开始属于进入页面时间

// 	// //协议返回耗时
// 	// int64UseTime := (time.Now().UnixNano() - lg.lg.user.ReqStartTime) / 1e6
// 	// lg.lg.user.AddReqUseTime("login-verifying", int64UseTime, lg.lg.user.ReqStartTime, time.Now().UnixNano())
// 	// lg.lg.receiveMap = true

// }

// func (lg *GatewayMsg) MsgProc_EnterSpaceNtf(msg *protoMsg.EnterSpaceNtf) {
// 	// //协议返回耗时
// 	// int64UseTime := (time.Now().UnixNano() - lg.lg.user.ReqStartTime) / 1e6
// 	// lg.lg.user.AddReqUseTime("login-EnterSpaceNtf", int64UseTime, lg.lg.user.ReqStartTime, time.Now().UnixNano())
// 	lg.lg.SetNext(GwStep_EnterSpace+1, 0)
// 	logger.Debug(internal.LogKey_RobotSpace, "enter space 角色：", lg.lg.user.RoleName, "EnterSpaceNtf msg:", msg)
// }
